package edu.ruc.cluster.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;

import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class Shellme {

	
	public static void main(String[] args){
		Shellme sm = new Shellme();
		String host = "root@dlnode27";
//		String cmd = "echo 1 |  /usr/sbin/update-alternatives --config java";
//		String version = "java -version";
		String cp = "cp /etc/hosts /etc/hosts-laaa";
		sm.runCmd(host, cp);
//		sm.runCmd(host, version);
	}
	
	public void runCmd(String host, String cmd){
		
		try{
			JSch jsch = new JSch();
			String user = host.substring(0, host.indexOf('@'));
			host = host.substring(host.indexOf('@') + 1);
			
			Session session = jsch.getSession(user, host, 22);
			
			UserInfo ui = new MyUserInfo();
			
			String passwd = "bigdata2012@DEKERUC";
			session.setPassword(passwd);
			session.setUserInfo(ui);
			session.connect(3000);
			
			ChannelShell channel = (ChannelShell)session.openChannel("shell");
			channel.setPty(true);
			channel.connect();
			channel.setOutputStream(new ByteArrayOutputStream());
//			ByteArrayOutputStream abc = new ByteArrayOutputStream()
//			channel.setOutputStream(new ByteArrayOutputStream());
			OutputStream writor = channel.getOutputStream();
			writor.write((cmd + "\r\n").getBytes());
//			writor.write("1\r\n".getBytes());			
		
			writor.flush();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
			char[] cbuf = new char[100];
			
			int ret = reader.read(cbuf);
			boolean end = false;
			int cnt = 1;
			while(ret != -1){
				String info = new String(cbuf, 0, ret);
				System.out.print("---" + info);
//				if(info.contains(s))
				if(info.contains("selection")){
					writor.write(("1" + "\r\n").getBytes());
					writor.flush();
				}else if(info.contains("[root")){
					writor.write("java -version\r\n".getBytes());
					writor.flush();
					if(cnt == 0)
						end = true;
					else 
						cnt --;
				}
				
				if(end == true){				
					writor.close();
					reader.close();
					session.disconnect();
					channel.disconnect();
					break;						
				}
				ret = reader.read(cbuf);
			}
			
//			String line;
//			while((line = reader.readLine()) != null){
//				System.out.println(line);
//				if(line.contains("selection")){
//					writor.write(("2\r\n").getBytes());
//					writor.flush();
//				}
//			}
			
			/*
			 // this method doesn't work, because if the stream is waiting some input, in.available() always blocks
			byte[] data = new byte[1024];
			InputStream in = channel.getInputStream();
			while(in.available() > 0){
				int len = in.read(data);
				String tmp = new String(data, 0, data.length);
				System.out.println(tmp);
				if(tmp.contains("selection")){
					writor.write(("2\r\n").getBytes());
					writor.flush();
				}
				if(len < 0){
					System.out.println("network error");
				}				
			}
			*/
			
		}catch(IOException e){
			e.printStackTrace();
		}catch(JSchException e){
			e.printStackTrace();
		}
	}
	public class MyUserInfo implements UserInfo{
		private String passwd = "";
		public String getPassphrase(){
			return null;
		}
		public String getPassword(){
			return passwd;
		}
		public boolean promptPassword(String message){
			passwd = "bigdata2012@DEKERUC";
			return true;
		}
		public boolean promptPassphrase(String message){
			return true;
		}
		public boolean promptYesNo(String message){
			return true;
		}
		public void showMessage(String message){
			System.out.println(message);
		}
	}
}
